home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / qtfullscreen.win / qtfullscreen.c next >
Encoding:
Text File  |  2000-10-06  |  6.9 KB  |  252 lines

  1. //////////
  2. //
  3. //    File:        QTFullScreen.c
  4. //
  5. //    Contains:    Functions to display full-screen QuickTime movies.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <4>         09/18/00    srk        added PumpNextNativeEvent function to allow the QT
  14. //                                  network code to execute (not having this was causing
  15. //                                  the PrePreroll function to never complete)
  16. //       <3>         04/30/99    rtm        added QTFullScreen_PlayMovieOnFullScreen, to play an existing
  17. //                                    movie full screen
  18. //       <2>         03/17/98    rtm        finally got back to this; now it's working on Mac and Windows
  19. //       <1>         12/22/97    rtm        first file
  20. //
  21. //    This file contains functions that illustrate how to play QuickTime movies full screen. The
  22. //    key elements to displaying full screen movies are the calls BeginFullScreen and EndFullScreen,
  23. //    introduced in QuickTime 2.5. In the function QTFullScreen_PlayOnFullScreen, we prompt the user
  24. //  for a movie, open that movie, configure it to play full screen, associate a movie controller,
  25. //    and then let the controller handle events. Your application should call the function
  26. //    QTFullScreen_EventLoopAction in its event loop (on MacOS) or when it gets idle events (on Windows).
  27. //
  28. //    In the function QTFullScreen_PlayMovieOnFullScreen, we take an existing movie and play it full
  29. //    screen; in this function, we use the Movie Toolbox to start the movie and to give it processor
  30. //    time.
  31. // 
  32. //////////
  33.  
  34. #include "QTFullScreen.h"
  35.  
  36. // gloabl variables
  37. WindowPtr                    gFullScreenWindow = NULL;        // the full-screen window
  38. MovieController                gMC = NULL;                        // movie controller for the full-screen window
  39. Ptr                            gRestoreState = NULL;            // restore state; used when closing the full-screen window
  40.  
  41.  
  42. //////////
  43. //
  44. // QTFullScreen_PlayOnFullScreen
  45. // Prompt the user for a movie and play it full screen.
  46. //
  47. //////////
  48.  
  49. OSErr QTFullScreen_PlayOnFullScreen (void)
  50. {
  51.     FSSpec                myFSSpec;
  52.     Movie                myMovie = NULL;
  53.     short                myRefNum = 0;
  54.     SFTypeList            myTypeList = {MovieFileType, 0, 0, 0};
  55.     StandardFileReply    myReply;
  56.     long                myFlags = fullScreenDontChangeMenuBar | fullScreenAllowEvents;
  57.     OSErr                myErr = noErr;
  58.     
  59.     StandardGetFilePreview(NULL, 1, myTypeList, &myReply);
  60.     if (!myReply.sfGood)
  61.         goto bail;
  62.     
  63.     // make an FSSpec record
  64.     FSMakeFSSpec(myReply.sfFile.vRefNum, myReply.sfFile.parID, myReply.sfFile.name, &myFSSpec);
  65.  
  66.     myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdPerm);
  67.     if (myErr != noErr)
  68.         goto bail;
  69.  
  70.     // now fetch the first movie from the file
  71.     myErr = NewMovieFromFile(&myMovie, myRefNum, NULL, NULL, newMovieActive, NULL);
  72.     if (myErr != noErr)
  73.         goto bail;
  74.     
  75.     CloseMovieFile(myRefNum);
  76.  
  77.     // set up for full-screen display
  78.     myErr = BeginFullScreen(&gRestoreState, NULL, 0, 0, &gFullScreenWindow, NULL, myFlags); 
  79.  
  80. #if TARGET_OS_WIN32
  81.     // on Windows, set a window procedure for the new window and associate a port with that window
  82.     QTMLSetWindowWndProc(gFullScreenWindow, QTFullScreen_HandleMessages);
  83.     CreatePortAssociation(GetPortNativeWindow(gFullScreenWindow), NULL, 0L);
  84. #endif
  85.     
  86.     SetMovieGWorld(myMovie, (CGrafPtr)gFullScreenWindow, GetGWorldDevice((CGrafPtr)gFullScreenWindow));
  87.     SetMovieBox(myMovie, &gFullScreenWindow->portRect);
  88.  
  89.     // create the movie controller
  90.     gMC = NewMovieController(myMovie, &gFullScreenWindow->portRect, 0);
  91.  
  92. bail:
  93.     return(myErr);
  94. }
  95.  
  96.  
  97. //////////
  98. //
  99. // QTFullScreen_RestoreScreen
  100. //
  101. //////////
  102.  
  103. OSErr QTFullScreen_RestoreScreen (void)
  104. {
  105.     OSErr        myErr = noErr;
  106.     
  107. #if TARGET_OS_WIN32    
  108.     DestroyPortAssociation((CGrafPtr)gFullScreenWindow);
  109. #endif
  110.  
  111.     DisposeMovieController(gMC);
  112.     myErr = EndFullScreen(gRestoreState, 0L); 
  113.     
  114.     return(myErr);
  115. }
  116.  
  117.  
  118. //////////
  119. //
  120. // QTFullScreen_EventLoopAction
  121. // Do any required event loop action processing.
  122. //
  123. //////////
  124.  
  125. OSErr QTFullScreen_EventLoopAction (EventRecord *theEvent)
  126. {
  127.     return(MCIsPlayerEvent(gMC, theEvent));
  128. }
  129.  
  130.  
  131. #if TARGET_OS_WIN32
  132. //////////
  133. //
  134. // QTFullScreen_HandleMessages
  135. // Handle Windows messages for the full-screen window.
  136. // 
  137. //////////
  138.  
  139. LRESULT CALLBACK QTFullScreen_HandleMessages (HWND theWnd, UINT theMessage, UINT wParam, LONG lParam)
  140. {
  141.     MSG                myMsg = {0};
  142.     EventRecord        myMacEvent;
  143.     LONG            myPoints = GetMessagePos();
  144.  
  145.     myMsg.hwnd = theWnd;
  146.     myMsg.message = theMessage;
  147.     myMsg.wParam = wParam;
  148.     myMsg.lParam = lParam;
  149.     myMsg.time = GetMessageTime();
  150.     myMsg.pt.x = LOWORD(myPoints);
  151.     myMsg.pt.y = HIWORD(myPoints);
  152.  
  153.     // translate the Windows message to a Mac event
  154.     WinEventToMacEvent(&myMsg, &myMacEvent);
  155.  
  156.     // pass the Mac event to the movie controller
  157.     QTFullScreen_EventLoopAction(&myMacEvent);
  158.         
  159.     return(DefWindowProc(theWnd, theMessage, wParam, lParam));
  160. }
  161. #endif    // TARGET_OS_WIN32
  162.  
  163.  
  164. //////////
  165. //
  166. // QTFullScreen_MoviePrePrerollCompleteProc
  167. // A completion procedure for pre-prerolling movies.
  168. //
  169. //////////
  170.  
  171. PASCAL_RTN void QTFullScreen_MoviePrePrerollCompleteProc (Movie theMovie, OSErr thePrerollErr, void *theRefcon)
  172. {
  173. #pragma unused(thePrerollErr, theRefcon)
  174.     StartMovie(theMovie);
  175. }
  176.  
  177.  
  178. //////////
  179. //
  180. // QTFullScreen_PlayMovieOnFullScreen
  181. // Play the specified movie full screen (without a movie controller).
  182. //
  183. //////////
  184.  
  185. OSErr QTFullScreen_PlayMovieOnFullScreen (Movie theMovie)
  186. {
  187.     long                myFlags = fullScreenAllowEvents;
  188.     GWorldPtr            myOrigGWorld = NULL;
  189.     Rect                myRect;
  190.     OSErr                myErr = noErr;
  191.     
  192.     StopMovie(theMovie);
  193.     
  194.     // set up for full-screen display
  195.     myErr = BeginFullScreen(&gRestoreState, NULL, 0, 0, &gFullScreenWindow, NULL, myFlags); 
  196.  
  197. #if TARGET_OS_WIN32
  198.     // on Windows, set a window procedure for the new window and associate a port with that window
  199.     QTMLSetWindowWndProc(gFullScreenWindow, QTFullScreen_HandleMessages);
  200.     CreatePortAssociation(GetPortNativeWindow(gFullScreenWindow), NULL, 0L);
  201. #endif
  202.     
  203.     GetMovieBox(theMovie, &myRect);
  204.     GetMovieGWorld(theMovie, &myOrigGWorld, NULL);
  205.     SetMovieGWorld(theMovie, (CGrafPtr)gFullScreenWindow, GetGWorldDevice((CGrafPtr)gFullScreenWindow));
  206.     SetMovieBox(theMovie, &gFullScreenWindow->portRect);
  207.             
  208.     PrePrerollMovie(theMovie, GetMovieTime(theMovie, NULL), GetMoviePreferredRate(theMovie), NewMoviePrePrerollCompleteProc(QTFullScreen_MoviePrePrerollCompleteProc), (void *)0L);    
  209.  
  210.     // play the movie through until the end; of course, a real application would probably want
  211.     // to call MoviesTask in its idle routine instead of just looping mindlessly here; this is
  212.     // left as an exercise for the reader
  213.     while (!IsMovieDone(theMovie))
  214.     {
  215.         PumpNextNativeEvent();
  216.         MoviesTask(theMovie, 0L);
  217.     }
  218.  
  219.     StopMovie(theMovie);
  220.     SetMovieGWorld(theMovie, myOrigGWorld, NULL);
  221.     
  222.     myErr = EndFullScreen(gRestoreState, 0L); 
  223.     
  224.     SetMovieBox(theMovie, &myRect);
  225.  
  226. bail:
  227.     return(myErr);
  228. }
  229.  
  230. //////////
  231. //
  232. // PumpNextNativeEvent
  233. // We need to service the Windows windowing messages, otherwise
  234. // the QuickTime network code can't execute (which will cause
  235. // PrePrerollMovie to never complete
  236. //
  237. //////////
  238.  
  239. void PumpNextNativeEvent(void)
  240. {
  241.     MSG msg;
  242.  
  243.     if (!PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  244.     {
  245.         return;
  246.     }
  247.     
  248.     PeekMessage(&msg,NULL,0,0,PM_REMOVE);
  249.  
  250.     TranslateMessage(&msg);
  251.     DispatchMessage(&msg);
  252. }